
/**
 * 
 * $Id: ConfUtil.java 126 2006-11-10 15:53:50Z  $ 
 * Created on 2005-12-29, 16:09:17
 */
package jm.lib.util;

import java.sql.Date;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.Locale;
import java.util.Map;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * get value form Properties, ResourceBundle or map, it will supply a default
 * value if the value is not exists or the value is not valid
 * 
 * @author JimingLiu
 * 
 */
public class ConfUtil {
	private static final Log logger = LogFactory.getLog(ConfUtil.class);
	
	private static final ResourceBundle getResourceBundle(
			String resourceBundle, Locale locale) {
		if(StrUtil.isEmpty(resourceBundle)) return null;
		ResourceBundle result;
		try {
			result = null==locale?ResourceBundle.getBundle(resourceBundle):ResourceBundle.getBundle(resourceBundle, locale);
		} catch (MissingResourceException e) {
			result = null;
			if(logger.isWarnEnabled()) {
				logger.warn("ResourceBundle [" + resourceBundle + "].["
						 + (null==locale?"":locale.toString()) +"] does not exists!" ,e);
			}
		}
		
		return result;
	}

	public static String getResourceValue(String resourceBundle, Locale locale, String key) {
		ResourceBundle rb = getResourceBundle(resourceBundle, locale);
		if(null == rb) return "";
		String result; 
		
		try {
			result = ResourceBundle.getBundle(resourceBundle).getString(key);
		} catch (MissingResourceException e) {
			result = "";
			if(logger.isWarnEnabled()) {
				logger.warn("Key [" + key
						+ "] does not exist in ResourceBundle [" + resourceBundle + "].["
						+ (null==locale?"":locale.toString()) +"]" ,e);
			}
		}
		
		return result;

	}
	
	public static String getResourceValue(String resourceBundle, String key) {
		return getResourceValue(resourceBundle, (Locale)null, key);
	}
	
	/**
	 * 
	 * @param resourceBundle
	 * @param locale
	 * @param key
	 * @param defaultValue
	 * @return
	 */
	public static String getResourceValue(String resourceBundle, Locale locale, String key, String defaultValue) {
		String result = getResourceValue(resourceBundle, locale, key);
		return StrUtil.isEmpty(result)?defaultValue:result;
	}
	
	public static final String getResourceValue(String resourceBundle, String key, String defaultValue) {
		return getResourceValue(resourceBundle, null, key, defaultValue);
	}
	
	public static final boolean getBoolean(Map m, Object key,
			boolean defaultValue) {
		return getBoolean((String) m.get(key), defaultValue);
	}
	
	public static final boolean getBoolean(String resourceBundle,
			Locale locale, String key, boolean defaultValue) {
		return getBoolean(getResourceValue(resourceBundle, locale, key), defaultValue);
	}
	
	public static final boolean getBoolean(String value, boolean defaultValue) {
		if (null == value || value.length() < 1)
			return defaultValue;
		char c = value.toString().charAt(0);
		if (c == 't' || c == 'T' || c == 'y' || c == 'Y' 
			|| (Character.isDigit(c) && '0' != c)) {
			return true;
		} else {
			return false;
		}
	}
	


	public static final byte getByte(Map m, Object key,
			byte defaultValue) {
		return getByte((String) m.get(key), defaultValue);
	}
	
	public static final byte getbyte(String resourceBundle,
			Locale locale, String key, byte defaultValue) {
		return getByte(getResourceValue(resourceBundle, locale, key), defaultValue);
	}
	
	public static final byte getByte(String value, byte defaultValue) {
		if (null == value || value.length() < 1)
			return defaultValue;
		byte result = defaultValue;
		try {
			result = Byte.parseByte(value);
		} catch (NumberFormatException e) {
			logger.warn("value ["+value+"] convert error!", e);
		}
		return result;
	}

	public static final short getShort(Map m, Object key,
			short defaultValue) {
		return getShort((String) m.get(key), defaultValue);
	}
	
	public static final short getShort(String resourceBundle,
			Locale locale, String key, short defaultValue) {
		return getShort(getResourceValue(resourceBundle, locale, key), defaultValue);
	}
	
	public static final short getShort(String value, short defaultValue) {
		if (null == value || value.length() < 1)
			return defaultValue;
		short result = defaultValue;
		try {
			result = Short.parseShort(value);
		} catch (NumberFormatException e) {
			logger.warn("value ["+value+"] convert error!", e);
		}
		return result;
	}

	public static final int getInt(Map m, Object key,
			int defaultValue) {
		return getInt((String) m.get(key), defaultValue);
	}
	
	public static final int getInt(String resourceBundle,
			Locale locale, String key, int defaultValue) {
		return getInt(getResourceValue(resourceBundle, locale, key), defaultValue);
	}
	
	public static final int getInt(String value, int defaultValue) {
		if (null == value || value.length() < 1)
			return defaultValue;
		int result = defaultValue;
		try {
			result = Integer.parseInt(value);
		} catch (NumberFormatException e) {
			logger.warn("value ["+value+"] convert error!", e);
		}
		return result;
	}

	public static final long getLong(Map m, Object key,
			long defaultValue) {
		return getLong((String) m.get(key), defaultValue);
	}
	
	public static final long getLong(String resourceBundle,
			Locale locale, String key, long defaultValue) {
		return getLong(getResourceValue(resourceBundle, locale, key), defaultValue);
	}
	
	public static final long getLong(String value, long defaultValue) {
		if (null == value || value.length() < 1)
			return defaultValue;
		long result = defaultValue;
		try {
			result = Long.parseLong(value);
		} catch (NumberFormatException e) {
			logger.warn("value ["+value+"] convert error!", e);
		}
		return result;
	}

		

	public static final float getFloat(Map m, Object key,
			float defaultValue) {
		return getFloat((String) m.get(key), defaultValue);
	}
	
	public static final float getFloat(String resourceBundle,
			Locale locale, String key, float defaultValue) {
		return getFloat(getResourceValue(resourceBundle, locale, key), defaultValue);
	}
	
	public static final float getFloat(String value, float defaultValue) {
		if (null == value || value.length() < 1)
			return defaultValue;
		float result = defaultValue;
		try {
			result = Float.parseFloat(value);
		} catch (NumberFormatException e) {
			logger.warn("value ["+value+"] convert error!", e);
		}
		return result;
	}


	public static final double getDouble(Map m, Object key,
			double defaultValue) {
		return getDouble((String) m.get(key), defaultValue);
	}
	
	public static final double getDouble(String resourceBundle,
			Locale locale, String key, double defaultValue) {
		return getDouble(getResourceValue(resourceBundle, locale, key), defaultValue);
	}
	
	public static final double getDouble(String value, double defaultValue) {
		if (null == value || value.length() < 1)
			return defaultValue;
		double result = defaultValue;
		try {
			result = Double.parseDouble(value);
		} catch (NumberFormatException e) {
			logger.warn("value ["+value+"] convert error!", e);
		}
		return result;
	}

	
	/**
	 * 
	 * @param m
	 * @param key
	 * @param defaultValue
	 * @return
	 */
	public static final Date getDate(Map m, Object key,
			Date defaultValue) {
		return getDate((String) m.get(key), defaultValue);
	}
	
	/**
	 * 
	 * @param resourceBundle
	 * @param locale
	 * @param key
	 * @param defaultValue
	 * @return
	 */
	public static final Date getDate(String resourceBundle,
			Locale locale, String key, Date defaultValue) {
		return getDate(getResourceValue(resourceBundle, locale, key), defaultValue);
	}
	
	/**
	 * 
	 * @param value a <code>String</code> object representing a date in 
     *        in the format "yyyy-mm-dd" or "yyyy/mm/dd"
	 * @param defaultValue
	 * @return
	 */
	public static final Date getDate(String value, Date defaultValue) {
		if (null == value || value.length() < 1)
			return defaultValue;
		Date result = defaultValue;
		String v = value.replace('/', '-');
		try {
			result = Date.valueOf(v);
		} catch (IllegalArgumentException e) {
			logger.warn("value ["+value+"] convert error!", e);
		}
		return result;
	}

		

	
	
	
	public static final Time getTime(Map m, Object key,
			Time defaultValue) {
		return getTime((String) m.get(key), defaultValue);
	}
	
	public static final Time getTime(String resourceBundle,
			Locale locale, String key, Time defaultValue) {
		return getTime(getResourceValue(resourceBundle, locale, key), defaultValue);
	}
	
	/**
	 * 
	 * @param value a <code>String</code> object representing a Time in 
     *        in the format "hh:mm:ss"
	 * @param defaultValue
	 * @return
	 */
	public static final Time getTime(String value, Time defaultValue) {
		if (null == value || value.length() < 1)
			return defaultValue;
		Time result = defaultValue;
		try {
			result = Time.valueOf(value);
		} catch (IllegalArgumentException e) {
			logger.warn("value ["+value+"] convert error!", e);
		}
		return result;
	}

	
	public static final Timestamp getTimestamp(Map m, Object key,
			Timestamp defaultValue) {
		return getTimestamp((String) m.get(key), defaultValue);
	}
	
	public static final Timestamp getTimestamp(String resourceBundle,
			Locale locale, String key, Timestamp defaultValue) {
		return getTimestamp(getResourceValue(resourceBundle, locale, key), defaultValue);
	}
	
	/**
	 * 
	 * @param value a <code>String</code> object representing a Timestamp in 
     *        in the format "yyyy-mm-dd hh:mm:ss.fffffffff"
	 * @param defaultValue
	 * @return
	 */
	public static final Timestamp getTimestamp(String value, Timestamp defaultValue) {
		if (null == value || value.length() < 1)
			return defaultValue;
		Timestamp result = defaultValue;
		String v = value.replace('/', '-');
		try {
			result = Timestamp.valueOf(v);
		} catch (IllegalArgumentException e) {
			logger.warn("value ["+value+"] convert error!", e);
		}
		return result;
	}

	public static final String getString(String s, String defaultValue) {
		return StrUtil.isEmpty(s)?defaultValue:s;
	}
	
	public static final String getString(Map m, String key, String defaultValue) {
		return getString((String)m.get(key), defaultValue);
	}

}


